﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
using Microsoft.Office.Server.Diagnostics;
using SharePoint.Intranet.LinkChecker.Functions;
using System.Xml;
using System.IO;

namespace SharePoint.Intranet.LinkChecker.TimerJobs
{
    public class LinkCheckerTimerJob : SPJobDefinition
    {

        public String SiteCollectionUrl { get; set; }

        public LinkCheckerTimerJob()
            : base()
        {
        }

        public LinkCheckerTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }

        public LinkCheckerTimerJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Link Checker";
        }


        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];

            SPSite site = contentDb.Sites[SiteCollectionUrl];

            SiteLinkChecker LinkChecker = new SiteLinkChecker();
            LinkChecker.CheckLinksInSite(site);

            
            FileStream BrokenLinksReport = File.Open(LinkChecker.GetBrokenLinkReportPath(), FileMode.Open);
            int FileLength = (int)BrokenLinksReport.Length;
            byte[] BrokenLinkReportBytes = new byte[FileLength];
            BrokenLinksReport.Read(BrokenLinkReportBytes, 0, FileLength);
            BrokenLinksReport.Close();
            BrokenLinksReport.Close();

            string NewFileName;
            using (SPWeb web = site.RootWeb)
            {
                NewFileName = string.Format("{1} - {0}.xml", web.Title, DateTime.Now.ToString("yyyyMMdd_HHmmss"));
            }



            SPWebServiceCollection webServices = new SPWebServiceCollection(SPFarm.Local);
            foreach (SPWebService webService in webServices)
            {
                foreach (SPWebApplication webApp in webService.WebApplications)
                {
                    if (webApp.IsAdministrationWebApplication)
                    {
                        using (SPSite CentralAdminSite = webApp.Sites[0])
                        {
                            using (SPWeb CentralAdminWeb = CentralAdminSite.RootWeb)
                            {
                                SPDocumentLibrary ReportsLibrary = (SPDocumentLibrary)CentralAdminWeb.Lists["Broken Link Scan Reports"];

                                string newUrl = CentralAdminSite.MakeFullUrl(ReportsLibrary.RootFolder.ServerRelativeUrl + "/" + NewFileName);
                                
                                
                                SPFile newFile = ReportsLibrary.RootFolder.Files.Add(newUrl, ms.ToArray());
                                newFile.Update();

                                SPListItem newItem = ReportsLibrary.Items.GetItemById(newFile.Item.ID);
                                newItem["URL"] = site.Url;
                                newItem.SystemUpdate();
                            }
                        }    
                    }
                }
            }

            site.Dispose();
        }

    }
}
